Lua utility module Timer
Basic timer - after a specified number of seconds, the specified thing happens.
Timers are updated automatically every frame before OnLoop.
Example usage:
local Timer = require("Engine.Timer") -- This will be called when the timer runs out LevelFuncs.FinishTimer = function(healthWhenStarted, victoryMessage) -- Open a door, display a message, make an explosion... whatever you wish DoSomething(healthWhenStarted, victoryMessage) end -- This function triggers the timer LevelFuncs.TriggerTimer = function(obj) local myTimer = Timer.Create("my_timer", 5.0, false, {minutes = false, seconds = true, deciseconds = true}, LevelFuncs.FinishTimer, Lara:GetHP(), "Well done!") myTimer:Start() end
Functions
Create(name, totalTime, loop, timerFormat, func[, ...]) | Create (but do not start) a new timer. |
Get(name) | Get a timer by its name. |
myTimer:SetFunction(func[, ...]) | Give the timer a new function and args |
myTimer:Start() | Begin or unpause a timer. |
myTimer:Stop() | Stop the timer. |
myTimer:IsActive() | Get whether or not the timer is active |
myTimer:SetPaused(p) | Pause or unpause the timer. |
myTimer:IsPaused() | Get whether or not the timer is paused |
myTimer:GetRemainingTime() | Get the remaining time for a timer. |
myTimer:SetRemainingTime(remainingTime) | Set the remaining time for a timer |
myTimer:GetTotalTime() | Get the total time for a timer. |
myTimer:SetTotalTime(totalTime) | Set the total time for a timer |
myTimer:SetLooping(looping) | Set whether or not the timer loops |
Functions
- Create(name, totalTime, loop, timerFormat, func[, ...])
-
Create (but do not start) a new timer.
You have the option of displaying the remaining time on the clock. Timer format details:
-- deciseconds are 1/10th of a second -- mins:secs local myTimeFormat1 = {minutes = true, seconds = true, deciseconds = false} -- also mins:secs local myTimeFormat2 = {minutes = true, seconds = true} -- secs:decisecs local myTimeFormat3 = {seconds = true, deciseconds = true} -- secs; also what is printed if you pass true instead of a table local myTimeFormat4 = {seconds = true}
Use this sparingly; in the classics, timed challenges did not have visible countdowns. For shorter timers, the gameplay benefit from showing the remaining time might not be necessary, and could interfere with the atmosphere of the level.
At any given time, only one timer can show its countdown.
Parameters:
- name string A label to give this timer; used to retrieve the timer later. Do not give your timers a name beginning with __TEN, as this is reserved for timers used by other internal libaries.
- totalTime number The duration of the timer, in seconds
- loop bool if true, the timer will start again immediately after the time has elapsed
- timerFormat table or bool If a table is given, the remaining time will be shown as a string, formatted according to the values in the table. If true, the remaining seconds, rounded up, will show at the bottom of the screen. If false, the remaining time will not be shown on screen.
- func func The LevelFunc function to call when the time is up
- ... a variable number of arguments with which the above function will be called (optional)
Returns:
-
Timer
The timer in its paused state
- Get(name)
-
Get a timer by its name.
Parameters:
- name string The label that was given to the timer when it was created
Returns:
-
Timer
The timer
- myTimer:SetFunction(func[, ...])
-
Give the timer a new function and args
Parameters:
- func function The LevelFunc member to call when the time is up
- ... a variable number of arguments with which the above function will be called (optional)
- myTimer:Start()
- Begin or unpause a timer. If showing the remaining time on-screen, its color will be set to white.
- myTimer:Stop()
- Stop the timer.
- myTimer:IsActive()
-
Get whether or not the timer is active
Returns:
-
bool
true if the timer is active, false if otherwise
- myTimer:SetPaused(p)
-
Pause or unpause the timer. If showing the remaining time on-screen, its color will be set to yellow (paused) or white (unpaused).
Parameters:
- p bool if true, the timer will be paused; if false, it would be unpaused
- myTimer:IsPaused()
-
Get whether or not the timer is paused
Returns:
-
bool
true if the timer is paused, false if otherwise
- myTimer:GetRemainingTime()
-
Get the remaining time for a timer.
Returns:
-
float
the time in seconds remaining on the clock
- myTimer:SetRemainingTime(remainingTime)
-
Set the remaining time for a timer
Parameters:
- remainingTime number the new time remaining for the timer
- myTimer:GetTotalTime()
-
Get the total time for a timer.
This is the amount of time the timer will start with, as well as when starting a new loop
Returns:
-
float
the timer's total time
- myTimer:SetTotalTime(totalTime)
-
Set the total time for a timer
Parameters:
- totalTime number timer's new total time
- myTimer:SetLooping(looping)
-
Set whether or not the timer loops
Parameters:
- looping bool whether or not the timer loops